/**
 * 
 */
package ejb.com.bbva.cbm.util;

import java.util.Properties;
import java.util.ResourceBundle;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.directory.InitialDirContext;

import org.apache.log4j.Logger;


/**
 * @author indra
 *
 */
public class LdapValidacionBBVA {
	static Logger logger = Logger.getLogger(LdapValidacionBBVA.class);
	private InitialDirContext ctx = null;
	/**
	 * @param args
	 */

	public void ejecuta()throws Exception
	 {
	     Properties ldapProperties = new Properties();
	     Properties env = System.getProperties();
	    /* FileInputStream inputStream = new FileInputStream("ldap.properties");
	     ldapProperties.load(inputStream);*/
	     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
	     //env.put(Context.PROVIDER_URL,ldapProperties.getProperty("ldap.url")+","+ldapProperties.getProperty("ldap.base"));
	     //env.put(Context.SECURITY_PRINCIPAL, ldapProperties.getProperty("ldap.username")+","+ldapProperties.getProperty("ldap.company")+","+ldapProperties.getProperty("ldap.base") );
	     //env.put(Context.SECURITY_CREDENTIALS,ldapProperties.getProperty("dap.password"));
	     env.put(Context.PROVIDER_URL,"ldap://192.168.1.11:389/dc=bbva,dc=com");
	     //env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=bbva,dc=com");
	     //env.put(Context.SECURITY_CREDENTIALS, "secret");
	     env.put(Context.SECURITY_PRINCIPAL, "cn=P014518,ou=banco,dc=bbva,dc=com");
	     env.put(Context.SECURITY_CREDENTIALS, "123456");
	     ctx = new InitialDirContext(env);
	     
	     logger.info(ctx.getNameInNamespace());
	     ctx.close();
	     logger.info("error:");

	 }
	public static boolean  autenticacion(String usuario, String password){
		try {
		     //ResourceBundle ldap = ResourceBundle.getBundle("ldap");
		     //String url       = ldap.getString("ldap.url");
		     //String empleados = ldap.getString("ldap.empleados");
		     String url       = CacheDatos.getInstance().getConfiguracionProperty(Constante.PARA_DETA_LDAP_URL);
		     String empleados = CacheDatos.getInstance().getConfiguracionProperty(Constante.PARA_DETA_LDAP_EMPLEADOS);
		     
		     logger.debug("Parametros del LDAP: (url="+url+" empleados="+empleados+")");
		     
		     /*
		     //System.out.println(env.get(Context.INITIAL_CONTEXT_FACTORY));
		     //System.out.println(env.get(Context.PROVIDER_URL));
		     //System.out.println(env.get(Context.SECURITY_PRINCIPAL));
		     //System.out.println(env.get(Context.SECURITY_CREDENTIALS));
		     */
		     		     
			 InitialDirContext ctx = null; 
		     Properties env = System.getProperties();
		     Properties envLDAP = (Properties) env.clone();
		     envLDAP.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		     envLDAP.put(Context.PROVIDER_URL,url);
		     envLDAP.put(Context.SECURITY_PRINCIPAL, "cn="+usuario+","+empleados);
		     envLDAP.put(Context.SECURITY_CREDENTIALS,password);
		     ctx = new InitialDirContext(envLDAP);
		     ctx.close();
		     
		     /*
		     
			 InitialDirContext ctx = null; 
		     Properties env = System.getProperties();
		     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
		     env.put(Context.PROVIDER_URL,url);
		     env.put(Context.SECURITY_PRINCIPAL, "cn="+usuario+","+empleados);
		     env.put(Context.SECURITY_CREDENTIALS,password);
		     ctx = new InitialDirContext(env);
		     ctx.close();
		     
		      * */
		     /* 
		     //FORMA 1
		     env.put(Context.PROVIDER_URL,"ldap://118.180.36.2:389/o=igrupobbva");
		     env.put(Context.SECURITY_AUTHENTICATION, "simple");
		     env.put(Context.SECURITY_PRINCIPAL, "cn="+usuario+",ou=empleados,o=igrupobbva");
		     env.put(Context.SECURITY_CREDENTIALS,password);
		     //FORMA 2
		     env.put(Context.PROVIDER_URL,"ldap://118.180.36.2:389/o=igrupobbva");
		     env.put(Context.SECURITY_AUTHENTICATION, "simple");
		     env.put(Context.SECURITY_PRINCIPAL, "cn=conneg,ou=usraplicaciones,o=igrupobbva");
		     env.put(Context.SECURITY_CREDENTIALS,"conneg123");
		     ctx = new InitialDirContext(env);
		     NamingEnumeration answer = ctx.search("ldap://118.180.36.2:389/ou=empleados,o=igrupobbva", "(sn="+usuario+")", null);
		     // Ahora exarninamos los resultados d e la busqueda
		     while (answer != null && answer.hasMore()) {
			     SearchResult sr = (SearchResult) answer.next();
			     String dn = sr.getName();
			     //System.out.println("Distinguished Name is " + dn);
			     Attributes attrs = sr.getAttributes();
			     
			     for (NamingEnumeration ne = attrs.getAll(); ne.hasMoreElements();) {
			    	 Attribute attr = (Attribute) ne.next();
			    	 String attrID = attr.getID();
			    	 //System.out.println(attrID + " ;");
			    	 for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) {
			    		 //System.out.println("\t" + vals.nextElement());
			    	 }
			     }
			     //System.out.println("\n");
		     }*/
		  }catch(AuthenticationException e){
			 e.printStackTrace();logger.error(e);
			 return false;
		  }catch (Exception e){
		     e.printStackTrace();logger.error(e);
		     return false;
		  }
		  return true;
	}
	public static void main(String[] args) {
		LdapValidacionBBVA.autenticacion("P014773", "karina02");
		//LdapValidacionBBVA.autenticacion("P006497", "P006497");
	}
}
